<?
	include ("conn.php");
	$db_selected = $_POST['db'];
	

	$db = get_conn($db_selected);

	
	$tabla = $_POST['tabla'];
	$sql = "show columns from ".$tabla; 
	$result = mysql_query($sql);
	if($result){
		$field_list = array();
		$field_list_type = array();	
		$field_list_key = array();			
			
		$i = 0;
		while ($row = mysql_fetch_array($result)){
			// nombre
			$field_list[$i]=$row['Field'];
			// tipo
			$tipo=$row['Type'];
			$tipo_array= explode('(',$tipo);			
			
			$tipo = $tipo_array[0];
			$field_list_type[$i]=$tipo;

			if($row['Key'] == 'PRI'){
				$field_list_key[$i]="P";
				$primary_key_name=$field_list[$i];
			}else{
				if($row['Key'] == 'MUL'){
					$field_list_key[$i]="M";
				}else{
					$field_list_key[$i]="R";
				}			
			}	

			$i++;
		}
	}else{
		error_handling(__FUNCTION__.$sql,mysql_error());
	}
	
	if($_POST['gtipo']=='HTML'){	
			/////////////				variables
			//////////////////////////////////////////////////////////////////////////////////////
			
			$variables_start=
				"<? session_start();
						\ninclude_once ('core/conn.php');
						\n\$db = get_conn();
						\ncheck_session();
						\nif(\$_GET['id'] != ''){
							\n\t\$sql = \" select * from ".$tabla." where ".$primary_key_name."=\".\$_GET['id']; 
							\n\t\$result = mysql_query(\$sql);	
							\n\tif(\$result){
								\n\t\t\$row = mysql_fetch_array(\$result);";
		
			$variables_end ="
							\n\t}else{
								\n\t\terror_handling(\$sql,mysql_error());
							\n\t}
						\n}
						
						
					\n?>
					
					\n<html>
					\n<head>
					
					";
					
			for($i= 0; $i < count($field_list); $i++){
				$variables_fields .= "\n\t\t\$".$field_list[$i]." = \$row['".$field_list[$i]."'];";
			}				
					
			$variables = $variables_start.$variables_fields.$variables_end;
			
			/////////////				javascript
			//////////////////////////////////////////////////////////////////////////////////////
				$javascript_start ="

				
				\n<script>
					\n<!--
						\n\tfunction checkdata(obj){";
				
				for($i= 0; $i < count($field_list); $i++){
					
					if($field_list_key!='P'){
						$fields_validation.="
							\n\t\tif(obj.".$field_list[$i].".value ==''){
								\n\t\t\talert('Please fill the ".$field_list[$i]."');
								\n\t\t\tobj.".$field_list[$i].".focus();
								\n\t\t\treturn false;
							\n\t\t}";				
					}
					
					if(isnumeric($field_list_type[$i])  ) {
						$fields_validation.="
							\n\t\tif(isNaN(obj.".$field_list[$i].".value) ){
								\n\t\t\talert('".$field_list[$i]." must be a number');
								\n\t\t\tobj.".$field_list[$i].".focus();
								\n\t\t\treturn false;
							\n\t\t}				
						";				
					}
				
				}
				
				
				$javascript_end="\n\treturn true;
						\n\t}
					\n-->
					\n</script>
					\n</head>									
					\n<body>														
				";	
				
				
			$javascript = $javascript_start.$fields_validation.$javascript_end;
		
			/////////////				lista
			//////////////////////////////////////////////////////////////////////////////////////
			$lista = "
			\n\t\t<? 
			\n\t\t\$sql = \"select ".$primary_key_name." as id,".$primary_key_name." as description from ".$tabla." \";
			\n\t\t\$result = mysql_query(\$sql);
			\n\t\tif(\$result){
				\n\t\t\t\$data =\"<table width='100%'>\";
				\n\t\t\tif(mysql_num_rows(\$result) > 0){
					\n\t\t\t\twhile(\$row = mysql_fetch_array(\$result)){
						\n\t\t\t\t\t\$data .=\"<tr>
											\n\t\t\t\t\t\t<td align='left' >
												\n\t\t\t\t\t\t\t<a href='?id=\".\$row['id'].\"'>\".\$row['description'].\"</a>
											\n\t\t\t\t\t\t</td>
										  \n\t\t\t\t\t\t</tr>\";
					\n\t\t\t\t}//while
				\n\t\t\t}//if
				\n\t\t\telse{
					\n\t\t\t\t\$data .= \"<tr><td align='center'>No hay ".$tabla." registrados</td></tr>\";			
				\n\t\t\t}
				\n\t\t\t\$data .=\"</table>\";
			\n\t\t}else{
				\n\t\t\terror_handling(\$sql,mysql_error());
			\n\t\t}
			\n\t\techo \$data;
			\n\t\t?>\n";
		
				
			/////////////				from
			//////////////////////////////////////////////////////////////////////////////////////
		
				$form_start="
				\n<form name='".$tabla."' method='post' action='/core/".$tabla.".php' onsubmit='return checkdata(this)'>
				\n<table width='100%' border='0'>
				  \n\t<tr>
					\n\t\t<td valign='top'>\n\t\t<table width='100%' border='0'>";
		
				$form_end=" \n\t\t\t<tr>
							\n\t\t\t\t<td colspan='2' align='center'>
							\n\t\t\t\t\t<?
							  \n\t\t\t\t\tif (\$_GET['id'] != \"\"){
								\n\t\t\t\t\t\techo \"<input type='submit' name='Actualizar' value='Actualizar'>\";
								\n\t\t\t\t\t\techo \"<input type='submit' name='Eliminar' value='Borrar'>\";										
								\n\t\t\t\t\t\techo \"<input type='submit' name='Nuevo' value='Cancelar'>\";
							  \n\t\t\t\t\t}else{
								\n\t\t\t\t\t\techo \"<input type='submit' name='Agregar' value='Agregar'>\";
							  \n\t\t\t\t\t}
							 \n\t\t\t\t\t?>
						\n\t\t\t\t</td>
						\n\t\t\t</tr> 
						\n\t\t</table>
						\n\t\t</td>
						\n\t\t<td width='50%' valign='top' align='center'>".$lista."
						\n\t\t</td>
					  \n\t</tr>
					\n</table>\n</form>
					\n</body></html>													
					?>
					";
		
				for($i= 0; $i < count($field_list); $i++){
					if($field_list_key[$i]=='P'){
						$inputs .="\n\t\t\t<tr>
									\n\t\t\t\t<td align='center'>
										\n\t\t\t\t\t&amp;nbsp;
									\n\t\t\t\t</td>					
		
									\n\t\t\t\t<td align='center'>
										\n\t\t\t\t\t<input type='hidden' name='".$field_list[$i]."' value='<? echo \$".$field_list[$i].";?>'>
									\n\t\t\t\t</td>					
								   \n\t\t\t</tr>";
							   
					}
					
					if($field_list_key[$i]=='R' || $field_list_key[$i]=='M'){
						$inputs .="\n\t\t\t<tr>
									\n\t\t\t\t<td align='center'>
										\n\t\t\t\t\t".$field_list[$i]."
									\n\t\t\t\t</td>					
						
									\n\t\t\t\t<td align='center'>
										\n\t\t\t\t\t<input type='text' name='".$field_list[$i]."' value='<? echo \$".$field_list[$i].";?>'>
									\n\t\t\t\t</td>					
								   \n\t\t\t</tr>";
					}			
				}
		
		
			$form = $form_start.$inputs.$form_end;
			$total = $variables.$javascript.$form;
			
			
	}
	
	
	
	
	
	
	
	
	
	
	
	
	if($_POST['gtipo']=='PHP'){	
	//////////////////////////////////////////////////////////////////////////////////////
	//////////////////	insert

	  $head = "<? \nsession_start();
				\ninclude ('conn.php');
				\n\$db = get_conn();
				\ncheck_session();";				  
				
			
		$insert = "
		  \nif (\$_POST['Agregar']!=\"\"){";
		
			for($i= 0; $i < count($field_list); $i++){
				$insert_fields .= "".$field_list[$i].",";

				if( $field_list_type[$i] =='int' ){
					$quotes_start = "\".";
					$quotes_end = ".\"";
					$format_funcion = "";					

				}else{
					$quotes_start = "\\\"\".";
					$quotes_end = ".\"\\\"";
					$format_funcion = "htmlspecialchars";										
				}

				$insert_values .= "\n\t".$quotes_start.$format_funcion."(\$_POST['".$field_list[$i]."'])".$quotes_end.",";				
			}				
 			$insert_fields = substr($insert_fields,0,strlen($insert_fields)-1);
 			$insert_values = substr($insert_values,0,strlen($insert_values)-1);

		$insert .= "\n\t\$sql = \"INSERT INTO ".$tabla." (".$insert_fields.") VALUES (".$insert_values.")\";
			\n\t\$result = mysql_query(\$sql);
			\n \tif(\$result){
			\n \t\tdo_redirect('../".$tabla.".php'); 	  
			\n \t}else{
			\n \t\terror_handling('".$tabla.".insert  - '.\$sql,mysql_error());
			\n \t}				  
		 \n}//Agregar";	
		 
		 	
	//////////////////////////////////////////////////////////////////////////////////////
	//////////////////	update
	//////////////////////////////////////////////////////////////////////////////////////

			for($i= 0; $i < count($field_list); $i++){

				if( $field_list_type[$i] =='int' ){
					$quotes_start = "";
					$quotes_end = "";
					$format_funcion = "";					

				}else{
					$quotes_start = "\\\"\".";
					$quotes_end = ".\"\\\"";
					$format_funcion = "htmlspecialchars";										
				}

				$update_fields .= $field_list[$i]."=".$quotes_start.$format_funcion."(\$_POST['".$field_list[$i]."'])".$quotes_end.",\n\t";				
			}				
 			$update_fields = substr($update_fields,0,strlen($update_fields)-1);


		$update = "
		  \nif (\$_POST['Actualizar']!=\"\"){
			\n\t\$sql = \"update ".$tabla." set \n\t".$update_fields." \n\twhere ".$primary_key_name."=\".\$_POST['".$primary_key_name."'];
			 
			\n\t\$result = mysql_query(\$sql);
			\n\tif(\$result){
				\n\t\tdo_redirect('../".$tabla.".php'); 	  
					
			\n\t}else{
				\n\t\terror_handling('".$tabla.".update - '.\$sql,mysql_error());
			\n\t}				  
		 \n}//update";
			

	//////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////
	//////////////////	delete
	//////////////////////////////////////////////////////////////////////////////////////
		$delete = "
		  \nif (\$_POST['Eliminar']!=\"\"){
			\n\t\$sql = \"delete from ".$tabla." where ".$primary_key_name."=\".\$_POST['".$primary_key_name."'];
			\n\t\$result = mysql_query(\$sql);
			\n\tif(\$result){
				\n\t\tdo_redirect('../".$tabla.".php'); 	  
			\n\t}else{
				\n\t\terror_handling('".$tabla.".delete - '.\$sql,mysql_error());
			\n\t}				  
		 \n}//delete";
	//////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////
	//////////////////	nuevo
	//////////////////////////////////////////////////////////////////////////////////////

		$nuevo = "
		  \nif (\$_POST['Nuevo']!=\"\"){
				\n\t\tdo_redirect('../".$tabla.".php'); 	  
		  \n}//nuevo";
		
		
		$total =$head.$insert.$update.$delete.$nuevo;
	}	
	//////////////////////////////////////////////////////////////////////////////////////



	if($_POST['gtipo']=='POST'){	
			for($i= 0; $i < count($field_list); $i++){
				$variables_fields .= "\n\t\t\$".$field_list[$i]." = \$_POST['".$field_list[$i]."'];";
			}				
					
			$total = $variables_fields;
	}
	
?>
<style type="text/css">
<!--
body {
	background-attachment: scroll;
	background-image: url(bg.jpg);
	background-repeat: repeat-x;
	background-position: left top;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
	color: #4A6D80;
	text-decoration: none;
	margin-top: 65px;
	margin-right: 0px;
	margin-bottom: 0px;
	margin-left: 0px;
	font-weight: normal;
	background-color: #4A6C7F;
}
#loginContainer {
	height: 522px;
	width: 451px;
	
	margin-left: auto;
	background-image: url(login_bg.jpg);
	background-repeat: no-repeat;
	margin-right: auto;
	margin-top: 0px;
	margin-bottom: 0px;
}
#login {
	padding-top: 150px;
	text-align: right;
	padding-right: 150px;
}
p {
	margin: 0px;
}
.textfield {
	height: 16px;
	border-right-width: 1px;
	border-bottom-width: 1px;
	border-right-style: solid;
	border-bottom-style: solid;
	border-bottom-color: #ABC1CD;
	border-right-color: #ABC1CD;
	font-family: Arial;
	font-size: 10px;
	font-weight: bold;
	color: #999999;
	text-decoration: none;
	width: 120px;
	padding-top: 3px;
	padding-bottom: 1px;
	padding-left: 5px;
	border-top-width: 0px;
	border-left-width: 0px;
	border-top-style: none;
	border-left-style: none;
}
.style1 {
	color: #FFFFFF;
	font-size: 18px;
}
-->
</style>
<div align="center">
	<a href="index.php?db=<? echo $db_selected?>&tabla=<? echo $tabla?>" class="style1">Back</a><br>
  <textarea name="textarea" style="background-color:#46697c; scrollbar-base-color:#000000; color:#FFFFFF; border:0px; background-image:url(gears-of-war-logo.png); background-position:center; background-repeat:no-repeat" id="textarea" cols="150" rows="40"><? echo $total;?></textarea>
</div>
